/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MyEngine;

import java.awt.Point;
import java.util.ArrayList;
import java.util.LinkedList;


/**
 * Class Node<T> - class describing node of tree with all needed methods
 * @author Macias based on http://kodatnik.blogspot.com/2010/02/drzewo-ogolna-implementacja.html
 */

public class Node {
     private Node parent;
     private LinkedList<Node> children;
     private ArrayList<int[]> moves;  //[0]-from.x ; [1]-from.y ; [2]-to.x [3]-to.y

     public Node() {
      parent = null;
      children = new LinkedList<Node>();
        moves = new ArrayList<int[]>();
     }

     public Node(Node parent) {
      this();
      this.parent = parent;
      children = new LinkedList<Node>();
        moves = new ArrayList<int[]>();
     }

    Node(Node parent, int[] move) {
        this.parent= parent;
        children = new LinkedList<Node>();
        parent.addChild(this);
        moves = new ArrayList<int[]>();
        moves.add(move);
    }


     public Node getParent(){
      return parent;
     }

     public void setParent(Node parent) {
      this.parent = parent;
     }


     public int getDegree() {
      return children.size();
     }

     public boolean isLeaf(){
      return children.isEmpty();
     }

     public Node addChild(Node child) {
      child.setParent(this);
      this.children.add(child);
      return child;
     }


     public Node getChild(int i){
      return children.get(i);
     }

     public Node removeChild(int i) {
      return children.remove(i);
     }

     public void removeChildren() {
      children.clear();
     }

     public LinkedList<Node> getChildren() {
      return children;
     }

     public Node getLeftMostChild() {
      if (children.isEmpty()) return null;
      return children.get(0);
     }

     public Node getRightSibling() {
      if (parent != null) {
       LinkedList<Node> parentsChildren = parent.getChildren();
       int pos = parentsChildren.indexOf(this);
       if (pos < (parentsChildren.size()-1))
        return parentsChildren.get(pos+1);
      }
      return null;
     }


    public void addMove(int[] move){
        this.moves.add(move);
    }
    public ArrayList<int[]> getMoves(){
        return this.moves;
    }
    public void setMoves(ArrayList<int[]> moves){
         System.arraycopy(moves, 0, this.moves, 0, moves.size());
    }

}
